"""
Case Type   : 资源池化
Case Name   : 收集日志信息指定主节点主机名称
Create At   : 2024.01
Owner       : chen
Description :
    1、指定主机点: gs_collector --begin-time="{date} --end-time="{date}" -h hostname
Expect      :
    1、收集正常
"""

import os
import unittest
import datetime
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.pri_node = Node('PrimaryDbUser')
        self.root_node = Node('PrimaryRoot')
        self.tmp_path = os.path.join(os.path.dirname(macro.PG_LOG_PATH))

    def test_shared_storage(self):
        text = '-----step1: 指定主节点: gs_collector --begin-time="\{date\}" ' \
            '--end-time="\{date\}" -h hostname' \
            ' expect: 收集正常-----'
        self.log.info(text)

        self.log.info('-----查看主机名称-----')
        check_cmd = f'hostname'
        self.log.info(check_cmd)
        hostname = self.pri_node.sh(check_cmd).result()
        self.log.info(hostname)

        self.log.info('-----收集日志信息-----')
        current_time = datetime.datetime.now()
        start_time = current_time.strftime('%Y%m%d %H:%M')
        end_time = (current_time + datetime.timedelta(minutes=5)).strftime('%Y%m%d %H:%M')

        self.log.info('-----收集日志信息指定主节点-----')
        collector_cmd = f'source {macro.DB_ENV_PATH};' \
                        f'gs_collector --begin-time="{start_time}" ' \
                        f'--end-time="{end_time}" -h {hostname}'
        self.log.info(collector_cmd)
        res = self.pri_node.sh(collector_cmd).result()
        self.log.info(res)
        self.assertIn(self.constant.GS_COLLECTOR_SUCCESS_MSG, res)
        
        self.log.info('-----查看结果-----')
        ls_cmd = f'''ls -al {self.tmp_path}'''
        self.log.info(ls_cmd)
        res = self.pri_node.sh(ls_cmd).result()
        self.log.info(res)
        find_cmd = f'find {self.tmp_path} -type f -name collector*.tar.gz ' \
                f'-cmin -1'''
        self.log.info(find_cmd)
        res = self.pri_node.sh(find_cmd).result()
        self.log.info(res)
        file_name = res.split('/')[-1]
        self.assertTrue(file_name.startswith('collector')
                        and file_name.endswith('.tar.gz'))
        tar_cmd = f'cd {self.tmp_path};tar -xvf {file_name}'
        self.log.info(tar_cmd)
        res = self.pri_node.sh(tar_cmd).result()
        self.log.info(res)
        self.assertIn('Summary.log', res)
        self.assertIn('Detail.log', res)
        self.assertIn(f'{hostname}.tar.gz', res)

    def tearDown(self):
        self.log.info('-----step2:清理环境-----')
        rm_cmd = f'''cd {self.tmp_path}; rm -rf collector_*'''
        self.log.info(rm_cmd)
        res = self.pri_node.sh(rm_cmd).result()
        self.log.info(res)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')